package spindles.gwt.server;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import spindles.api.domain.Person;
import spindles.api.domain.SamplingRate;
import spindles.api.domain.SessionPart;
import spindles.api.domain.SleepSession;
import spindles.api.util.Util;
import spindles.gwt.shared.EpochDTO;
import spindles.gwt.shared.GWTException;
import spindles.gwt.shared.PersonDTO;
import spindles.gwt.shared.SleepDTO;
import spindles.gwt.shared.SleepPartDTO;
import spindles.gwt.shared.SpindleDTO;
import spindles.gwt.shared.SpindlesManager;

public class SpindlesServletStub extends StubServlet implements
		SpindlesManager {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 6411075554947370903L;
	
	public String getExportURL(String level, long id, boolean soft){
		return "/test/spindles.tsv";
	}

	@SuppressWarnings("unchecked")
	public List getPersons() {
		Person p = new Person();
		p.setId(1);
		p.setComments("");
		p.setFirstName("Firstname");
		p.setLastName("Lastname");
		
		SortedSet<Person> persons = new TreeSet<Person>();
		persons.add(p);
		
		return ServletUtil.map(persons, PersonDTO.class);
	}

	@SuppressWarnings("unchecked")
	public List getSleeps(long personID) {
		SleepSession sleep = new SleepSession();
		sleep.setId(2);
		sleep.setPersonID(1);
		sleep.setDate(Util.toDate("1/1/2007", "dd/MM/yyyy"));
		sleep.setSamplingRate(SamplingRate.SR256);
		
		SessionPart part = new SessionPart();
		part.setId(3);
		part.setSleepSessionID(2);
		part.setChannel("Cz");
		part.setStart(Util.toDate("1/1/2007 1:10:00", "dd/MM/yyyy HH:mm:ss"));
		part.setEnd(Util.toDate("1/1/2007 1:15:04", "dd/MM/yyyy HH:mm:ss"));
		
		SessionPart part1 = new SessionPart();
		part1.setId(200);
		part1.setSleepSessionID(2);
		part1.setChannel("C3");
		part1.setStart(Util.toDate("1/1/2007 1:10:00", "dd/MM/yyyy HH:mm:ss"));
		part1.setEnd(Util.toDate("1/1/2007 1:15:04", "dd/MM/yyyy HH:mm:ss"));
		
		SortedSet<SleepSession> sleeps = new TreeSet<SleepSession>();
		sleeps.add(sleep);
		List result = new ArrayList();

		for (SleepSession s : sleeps) {
			SortedSet<SessionPart> parts = new TreeSet<SessionPart>();
			parts.add(part);
			parts.add(part1);
			List partDTOs = ServletUtil.map(parts, SleepPartDTO.class);

			SleepDTO sleepDTO = (SleepDTO) ServletUtil.map(s,
					SleepDTO.class);
			sleepDTO.setSleepParts(partDTOs);
			result.add(sleepDTO);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List getEpochs(long sleepPartID) {
		EpochDTO epoch = new EpochDTO();
		epoch.setId("4");
		epoch.setSessionPartID(String.valueOf(sleepPartID));
		epoch.setStartTime("1:10:00");
		epoch.setEndTime("1:10:16");
		epoch.setSoftSpindlesCount(1);
		epoch.setHardSpindlesCount(1);
		
		
		
		SpindleDTO spindle1 = new SpindleDTO();
		spindle1.setId("5");
		spindle1.setAbsoluteStartTime("1:10:10.244");
		spindle1.setRelativeStartTime("10.244");
		spindle1.setAbsoluteEndTime("1:10:12:244");
		spindle1.setRelativeEndTime("12:244");
		spindle1.setSpindleDuration("2");
		spindle1.setCriterion("Soft");
		spindle1.setIsiDuration("0");
		
		SpindleDTO spindle3 = new SpindleDTO();
		spindle3.setId("5");
		spindle3.setAbsoluteStartTime("1:10:10.244");
		spindle3.setRelativeStartTime("10.244");
		spindle3.setAbsoluteEndTime("1:10:12:244");
		spindle3.setRelativeEndTime("12:244");
		spindle3.setSpindleDuration("2");
		spindle3.setCriterion("Soft");
		spindle3.setIsiDuration("0");
		
		SpindleDTO spindle2 = new SpindleDTO();
		spindle2.setId("7");
		spindle2.setAbsoluteStartTime("1:10:10.244");
		spindle2.setRelativeStartTime("10.244");
		spindle2.setAbsoluteEndTime("1:10:12:244");
		spindle2.setRelativeEndTime("12:244");
		spindle2.setSpindleDuration("2");
		spindle2.setCriterion("Hard");
		spindle2.setIsiDuration("0");
		
		List softSpindles = new ArrayList();
		softSpindles.add(spindle1);
		softSpindles.add(spindle3);
		
		List hardSpindles = new ArrayList();
		hardSpindles.add(spindle2);		
		
		epoch.setSoftSpindles(softSpindles);		
		epoch.setHardSpindles(hardSpindles);
		
		List epochs = new ArrayList();
		epochs.add(epoch);
		return epochs;
	}
	
	public String getExportPath(int type, long personID, boolean soft) throws GWTException{
		return null;
	}

	public String getExportURL(long arg0, SpindleDTO arg1, int arg2) {
		// TODO Auto-generated method stub
		return null;
	}

}
